package org.light.reflecttable.facade;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.light.reflecttable.serviceimpl.TableServiceImpl;
import org.light.reflecttable.utils.StringUtil;

import net.sf.json.JSONObject;

public class LookupDbSettingsFacade extends HttpServlet{
	private static final long serialVersionUID = -8271513477395035931L;

	protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		response.setContentType("application/json;charset=UTF-8");
		Map<String,Object> result = new TreeMap<String,Object>();
		PrintWriter out = response.getWriter();
		Map<String,String> settings = new HashMap<>();
		settings.put("dbType",TableServiceImpl.dbType);
		settings.put("dbLink",TableServiceImpl.dbLink);
		settings.put("dbHost",TableServiceImpl.dbHost);
		settings.put("dbPort",TableServiceImpl.dbPort);
		if (!StringUtil.isBlank(TableServiceImpl.dbName)) {
			settings.put("dbName",TableServiceImpl.dbName);
		} else {
			settings.put("dbName","");
		}
		settings.put("userName",TableServiceImpl.userName);
		settings.put("password",TableServiceImpl.password);
		settings.put("topn",""+TableServiceImpl.topn);
		result.put("success",true);
		result.put("rows",settings);
		out.print(JSONObject.fromObject(result).toString());
	}

	public String getServletInfo(){
		return "Powered by Mind Rules.";
	}

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

}
